﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sheeps
{
    /// <summary>
    /// Represents game state
    /// </summary>
    public class GameState : ICloneable
    {
        private double width;
        private double height;

        private void UpdatePenCenter()
        {
            this.PenCenter = new Vector3(this.Width / 2, this.Height / 2, 0);
        }

        /// <summary>
        /// List of sheep taking part in simulation
        /// </summary>
        public List<Sheep> SheepList
        {
            get;
            set;
        }

        /// <summary>
        /// Width of pasture
        /// </summary>
        public double Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
                this.UpdatePenCenter();
            }
        }

        /// <summary>
        /// Height of pasture
        /// </summary>
        public double Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
                this.UpdatePenCenter();
            }
        }

        /// <summary>
        /// Dog
        /// </summary>
        public Dog Dog
        {
            get;
            set;
        }

        public Vector3 PenCenter
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates new instance of GameState
        /// </summary>
        /// <param name="width">Pasture width</param>
        /// <param name="height">Pasture height</param>
        public GameState(double width, double height)
        {
            this.Width = width;
            this.Height = height;
            this.SheepList = new List<Sheep>();
            this.PenSize = 50;
        }

        #region ICloneable Members

        public object Clone()
        {
            GameState ngs = (GameState)this.MemberwiseClone();
            ngs.SheepList = new List<Sheep>();
            foreach (var sheep in this.SheepList)
            {
                ngs.SheepList.Add((Sheep)sheep.Clone());
            }
            ngs.Dog = (Dog)this.Dog.Clone();
            return ngs;
        }

        #endregion

        public double PenSize
        {
            get;
            set;
        }

        public bool IsSheepInPen(Sheep sheep)
        {
            if (sheep.Position.Distance(this.PenCenter) < this.PenSize / 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int CountSheepInPen()
        {
            int cnt = 0;
            foreach (var sheep in this.SheepList)
            {
                if (this.IsSheepInPen(sheep))
                {
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
